from django.db import models
from pyceab.course.models import Course

# Create your models here.

START_MINS = 30
START_HRS = 8
PX_PER_BLOCK = 5

OFFERING_TYPE_CHOICES = (
    ('TUT', 'Tutorial'),
    ('LEC', 'Lecture'),
    ('LAB', 'Laboratory'),
)

class Offering(models.Model):
    course = models.ForeignKey(Course)
    type = models.CharField(max_length=3, choices=OFFERING_TYPE_CHOICES)
    section = models.PositiveIntegerField()
    term = models.PositiveIntegerField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return u' '.join(map(unicode, [self.course, self.term,  self.type]))

class TimeSlot(models.Model):
    offering = models.ForeignKey(Offering)
    day = models.CharField(max_length=2)
    start_time = models.PositiveIntegerField()
    end_time = models.PositiveIntegerField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return u' '.join(map(unicode, [
            self.offering, 
            self.day, 
            self.start_time, 
            self.end_time]))

    @property
    def start_hr(self):
        return int(str(self.start_time)[:-2])

    @property
    def end_hr(self):
        return int(str(self.end_time)[:-2])

    @property
    def start_min(self):
        return int(str(self.start_time)[-2:])

    @property
    def end_min(self):
        return int(str(self.end_time)[-2:])

    @property
    def duration(self):
        mins = self.end_min - self.start_min
        hrs = self.end_hr - self.start_hr
        return hrs*60 + mins

    @property
    def start_offset(self):
        mins = self.start_min - START_MINS
        hrs = self.start_hr - START_HRS
        return hrs*60 + mins

    @property
    def top(self):
        return self.start_offset/10*PX_PER_BLOCK

    @property
    def height(self):
        return self.duration/10*PX_PER_BLOCK


class Schedule(models.Model):
    offerings = models.ManyToManyField(Offering)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return u'schedule id:%d' % self.id


